#!/usr/bin/env python
# coding: utf-8

###############################################################
#   计划执行服务类
###############################################################


import urllib.parse
import urllib.request
import logging
import json
import uuid
import datetime
import time
from traceback import print_exc
import logging
from sqlalchemy import *
from decimal import Decimal
import requests

from utils import Utils
from ding_helper import DingHelper


# 平台结算配置

logger = logging.getLogger()

headers = {
    'X-Requested-With': 'XMLHttpRequest',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}


PERIOD_5_HOT_OR_COLD_NUM = 150


LAST_HOT_OR_COLD_5CODES_DATA = {
    'notify_expect': 749559,
    'expect': 749559

}


class SchedulerApi(object):
    def __init__(self):
        self.CONTINUOUS_LOSE_NUMBER_FOR_NOTIFY = 7  # 168 冷热轮动连挂次数设置

    def change_lose_num(self, lose_num):
        self.CONTINUOUS_LOSE_NUMBER_FOR_NOTIFY = lose_num  # 168 冷热轮动连挂次数设置

    def watch_5codes_hot_cold(self):
        logger.info('进入5码冷热轮动监控周期')
        """ 历史数据 """
        url = 'https://www.52cb1.com/pk10/api/history'

        date = Utils.int2str_time(int(time.time()))
        data = {
            "period": (None, str(PERIOD_5_HOT_OR_COLD_NUM)),
            "date": (None, date),
            "filter": (None, "0"),
            "type": (None, "1"),
            "lottery": (None, "xyft168")
        }
        print('lose_num', self.CONTINUOUS_LOSE_NUMBER_FOR_NOTIFY)
        try:
            res = requests.post(url, headers=headers, files=data, cookies=None, timeout=5)
            d = json.loads(res.text)
            if d['status'] == 1:
                history_codes_list_source = d['result']
                # history_codes_list_source.reverse()
                last_codes_expect = history_codes_list_source[0]['expect']
                print(last_codes_expect)
                print(len(history_codes_list_source))
                match_one_list = []
                match_two_list = []
                match_three_list = []
                match_four_list = []
                match_five_list = []
                match_six_list = []
                match_seven_list = []
                match_eight_list = []
                match_nine_list = []
                match_ten_list = []
                if LAST_HOT_OR_COLD_5CODES_DATA['expect'] == last_codes_expect:
                    pass
                else:
                    LAST_HOT_OR_COLD_5CODES_DATA['expect'] = last_codes_expect

                    for num in range(1, 11):

                        key_index = 'num' + str(num)
                        for i in range(0, PERIOD_5_HOT_OR_COLD_NUM-3):
                            base_fixed_code = history_codes_list_source[i][key_index]

                            base_hot_or_cold_group = set()
                            base_segment_codes_list = history_codes_list_source[i+1:]
                            for base_element in base_segment_codes_list:
                                base_hot_or_cold_group.add(base_element[key_index])
                                if len(base_hot_or_cold_group) > 4:
                                    break

                            pre_fixed_code = history_codes_list_source[i+1][key_index]
                            # print('fixed_curr_codes', fixed_curr_codes)
                            pre_hot_or_cold_group = set()
                            pre_segment_codes_list = history_codes_list_source[i+2:]
                            for element in pre_segment_codes_list:
                                pre_hot_or_cold_group.add(element[key_index])
                                if len(pre_hot_or_cold_group) > 4:
                                    break

                            if pre_fixed_code in pre_hot_or_cold_group:
                                if base_fixed_code not in base_hot_or_cold_group:

                                    if num == 1:
                                        match_one_list.append(i)
                                    elif num == 2:
                                        match_two_list.append(i)
                                    elif num == 3:
                                        match_three_list.append(i)
                                    elif num == 4:
                                        match_four_list.append(i)
                                    elif num == 5:
                                        match_five_list.append(i)
                                    elif num == 6:
                                        match_six_list.append(i)
                                    elif num == 7:
                                        match_seven_list.append(i)
                                    elif num == 8:
                                        match_eight_list.append(i)
                                    elif num == 9:
                                        match_nine_list.append(i)
                                    else:
                                        match_ten_list.append(i)
                            else:
                                if base_fixed_code in base_hot_or_cold_group:
                                    if num == 1:
                                        match_one_list.append(i)
                                    elif num == 2:
                                        match_two_list.append(i)
                                    elif num == 3:
                                        match_three_list.append(i)
                                    elif num == 4:
                                        match_four_list.append(i)
                                    elif num == 5:
                                        match_five_list.append(i)
                                    elif num == 6:
                                        match_six_list.append(i)
                                    elif num == 7:
                                        match_seven_list.append(i)
                                    elif num == 8:
                                        match_eight_list.append(i)
                                    elif num == 9:
                                        match_nine_list.append(i)
                                    else:
                                        match_ten_list.append(i)

                    print(match_one_list)
                    print(match_two_list)
                    print(match_three_list)
                    print(match_four_list)
                    print(match_five_list)
                    print(match_six_list)
                    print(match_seven_list)
                    print(match_eight_list)
                    print(match_nine_list)
                    print(match_ten_list)
                    lose_index = self.CONTINUOUS_LOSE_NUMBER_FOR_NOTIFY-1

                    if (match_one_list[0] == 0 and match_one_list[lose_index] == lose_index) or \
                       (match_two_list[0] == 0 and match_two_list[lose_index] == lose_index) or \
                       (match_three_list[0] == 0 and match_three_list[lose_index] == lose_index) or \
                       (match_four_list[0] == 0 and match_four_list[lose_index] == lose_index) or \
                       (match_five_list[0] == 0 and match_five_list[lose_index] == lose_index) or \
                       (match_six_list[0] == 0 and match_six_list[lose_index] == lose_index) or \
                       (match_seven_list[0] == 0 and match_seven_list[lose_index] == lose_index) or \
                       (match_eight_list[0] == 0 and match_eight_list[lose_index] == lose_index) or \
                       (match_nine_list[0] == 0 and match_nine_list[lose_index] == lose_index) or \
                       (match_ten_list[0] == 0 and match_ten_list[lose_index] == lose_index):
                        if LAST_HOT_OR_COLD_5CODES_DATA['notify_expect'] != last_codes_expect:

                            fix_codes = []
                            if match_one_list[0] == 0 and match_one_list[lose_index] == lose_index:
                                fix_codes.append('冠')
                            if match_two_list[0] == 0 and match_two_list[lose_index] == lose_index:
                                fix_codes.append('亚')
                            if match_three_list[0] == 0 and match_three_list[lose_index] == lose_index:
                                fix_codes.append('三')
                            if match_four_list[0] == 0 and match_four_list[lose_index] == lose_index:
                                fix_codes.append('四')
                            if match_five_list[0] == 0 and match_five_list[lose_index] == lose_index:
                                fix_codes.append('五')
                            if match_six_list[0] == 0 and match_six_list[lose_index] == lose_index:
                                fix_codes.append('六')
                            if match_seven_list[0] == 0 and match_seven_list[lose_index] == lose_index:
                                fix_codes.append('七')
                            if match_eight_list[0] == 0 and match_eight_list[lose_index] == lose_index:
                                fix_codes.append('八')
                            if match_nine_list[0] == 0 and match_nine_list[lose_index] == lose_index:
                                fix_codes.append('九')
                            if match_ten_list[0] == 0 and match_ten_list[lose_index] == lose_index:
                                fix_codes.append('十')

                            ding_content = '168 冷热轮动通知提醒：{}   连挂{}期  当前第{}期'.\
                                format(" ".join(fix_codes), self.CONTINUOUS_LOSE_NUMBER_FOR_NOTIFY, last_codes_expect)
                            print(ding_content)
                            DingHelper.ding_kucun(DingHelper, ding_content)
                            LAST_HOT_OR_COLD_5CODES_DATA['notify_expect'] == last_codes_expect

        except requests.exceptions.ConnectionError as e:
            print("Error", e.args)
        return None